package com.jinlia.show.modules.base.wxapp.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.constant.OrderPayStatusEnum;
import com.jinlia.show.modules.base.constant.PayForTypeEnum;
import com.jinlia.show.modules.base.constant.PayMethodTypeEnum;
import com.jinlia.show.modules.base.constant.PayResultStatusEnum;
import com.jinlia.show.modules.base.model.query.PayOrderQuery;
import com.jinlia.show.modules.base.system.entity.MemberCard;
import com.jinlia.show.modules.base.system.entity.Order;
import com.jinlia.show.modules.base.system.service.MemberCardBalanceChangeMapperService;
import com.jinlia.show.modules.base.system.service.MemberCardMapperService;
import com.jinlia.show.modules.base.system.service.OrderMapperService;
import com.jinlia.show.modules.base.wxapp.entity.PayOrder;
import com.jinlia.show.modules.base.wxapp.entity.PayOrderRecord;
import com.jinlia.show.modules.base.wxapp.mapper.PayOrderRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class PayOrderRecordMapperService extends ServiceImpl<PayOrderRecordMapper, PayOrderRecord> {
    @Autowired
    private PayOrderMapperService payOrderMapperService;
    @Autowired
    private OrderMapperService orderMapperService;
    @Autowired
    private MemberCardMapperService memberCardMapperService;
    @Autowired
    private MemberCardBalanceChangeMapperService memberCardBalanceChangeMapperService;

    /**
     * 通过支付单创建记录
     *
     * @param payOrder
     * @return
     */
    public PayOrderRecord createByPayOrder(PayOrder payOrder) {
        PayOrderRecord payOrderRecord = new PayOrderRecord();
        payOrderRecord.setPayOrderId(payOrder.getId());
        payOrderRecord.setPayAmount(payOrder.getPayAmount());
        payOrderRecord.setPayStatus(PayResultStatusEnum.USERPAYING.getValue());
        payOrderRecord.setPayUserId(UserInfoUtils.getUserId());
        payOrderRecord.setPayUserName(UserInfoUtils.getUserName());
        this.save(payOrderRecord);
        return payOrderRecord;
    }

    public Page<PayOrderRecord> getPage(PayOrderQuery payOrderQuery) {
        Page<PayOrderRecord> page = new Page<>(payOrderQuery.getCurrent(), payOrderQuery.getSize());
        Page<PayOrderRecord> page1 = this.page(page);
        return page1;
    }

    public PayOrderRecord addPayOrderRecord(PayOrderRecord payOrderRecord) {
        PayOrder payOrder = payOrderMapperService.getById(payOrderRecord.getPayOrderId());
        if (payOrder.getPaidAmount().compareTo(payOrder.getPayAmount()) >= 0) {
            throw new RuntimeException("支付单已支付完成,无需再支付");
        }
        //剩余待付金额
        BigDecimal subtract = payOrder.getPayAmount().subtract(payOrder.getPaidAmount());
        if (payOrderRecord.getPayAmount().compareTo(subtract) > 0) {
            throw new RuntimeException("支付金额超出待付金额");
        }
        payOrderRecord.setPayStatus(PayResultStatusEnum.SUCCESS.getValue());
        payOrderRecord.setPayUserId(UserInfoUtils.getUserId());
        payOrderRecord.setPayUserName(UserInfoUtils.getUserName());
        if (PayMethodTypeEnum.BALANCE_PAY.getValue().equals(payOrderRecord.getPayMethodType())) {
            //会员卡余额 支付
            MemberCard byIdMemberCard = memberCardMapperService.getById(payOrderRecord.getMemberCardId());
            memberCardBalanceChangeMapperService.balancePayOrder(
                    payOrderRecord.getPayOrderId(), //支付单
                    payOrderRecord.getPayAmount(), //金额
                    byIdMemberCard);//会员卡
        }else if (PayMethodTypeEnum.SCAN.getValue().equals(payOrderRecord.getPayMethodType())) {
            //扫码 TODO
        } else {
            //TODO
        }
        payOrderRecord.setPayMethodType(PayMethodTypeEnum.OFFLINE.getValue());
        this.save(payOrderRecord);
        //修改支付单
        payOrder.setPaidAmount(payOrder.getPaidAmount().add(payOrderRecord.getPayAmount()));
        if (payOrder.getPaidAmount().compareTo(payOrder.getPayAmount()) >= 0) {
            payOrder.setPayOrderStatus(OrderPayStatusEnum.ALL_PAY.getValue());
            //处理原始的
            afterPayOrderOk(payOrder);
        } else {
            payOrder.setPayOrderStatus(OrderPayStatusEnum.PART_PAY.getValue());
        }
        payOrderMapperService.updateById(payOrder);
        return payOrderRecord;
    }

    /**
     * 支付单完成后事项
     *
     * @param payOrder
     */
    public void afterPayOrderOk(PayOrder payOrder) {
        Integer payForType = payOrder.getPayForType();
        if (PayForTypeEnum.SALE.getValue().equals(payForType)) {
            Order byId = orderMapperService.getById(payOrder.getPayForId());
            byId.setPayStatus(OrderPayStatusEnum.ALL_PAY.getValue());
            orderMapperService.updateById(byId);
        }else if (PayForTypeEnum.MEMBER_RECHARGE.getValue().equals(payForType)) {
            //会员充值
        }

    }
}
